'''Game piece definitions'''

import game_constants 

class GamePiece(object):
    valid_names = set()
    
    def __init__(self, name, location):
        if name not in self.valid_names:
            raise Exception('Invalid name %s' % name)
        
        self.name = name
        self.location = location

    def move_piece(self, loc_to, contiguous = False):
        '''Update game_piece position to loc_to.  contiguous == True indicates whether the piece must move contiguously'''

        if not self.can_move_to(loc_to, contiguous):
            raise Exception('Cannot move %s to %s%s' % (repr(self), repr(loc_to), ' contiguously' if contiguous else ''))

        self.location = loc_to
           
    def get_location(self):
        return self.location

    def can_move_to(self, loc_to, congiguous):
        return True

    def __repr__(self):
        return '[%s: %s]' % (self.__class__.__name__, self.name)


class PlayerPiece(GamePiece):
    valid_names = set(game_constants.g_characters.iterkeys())
    
    def __init__(self, name, location = None):
        super(PlayerPiece, self).__init__(name, location)

    def can_move_to(self, loc_to, contiguous):
        return (not contiguous) or loc_to.can_be_reached(self.location)

    def __repr__(self):
        return 'Character: %s' % (self.name)


class WeaponPiece(GamePiece):
    valid_names = game_constants.g_weapons
    
    def __init__(self, name, location = None):
        super(WeaponPiece, self).__init__(name, location)
    def __repr__(self):
        return 'Weapon: %s' % (self.name)
        

if __name__ == '__main__':
    pass


